package com.knowledgeqa.system.service.impl;

import com.knowledgeqa.system.entity.Question;
import com.knowledgeqa.system.mapper.QuestionMapper;
import com.knowledgeqa.system.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 问题服务实现类
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public boolean publishQuestion(Question question) {
        // 设置默认值
        question.setStatus(0); // 待解决状态
        question.setCreateTime(new Date());
        question.setUpdateTime(new Date());
        
        return questionMapper.insert(question) > 0;
    }

    @Override
    public Question findById(Long id) {
        return questionMapper.selectById(id);
    }

    @Override
    public List<Question> findByUserId(Long userId) {
        return questionMapper.selectByUserId(userId);
    }

    @Override
    public List<Question> findPage(Map<String, Object> params) {
        return questionMapper.selectPage(params);
    }

    @Override
    public int findCount(Map<String, Object> params) {
        return questionMapper.selectCount(params);
    }

    @Override
    public boolean update(Question question) {
        question.setUpdateTime(new Date());
        return questionMapper.update(question) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        // 实际业务中可能需要先检查是否有回答
        return questionMapper.deleteById(id) > 0;
    }

    @Override
    public boolean closeQuestion(Long id) {
        // 设置问题状态为已关闭
        return questionMapper.updateStatus(id, 2) > 0;
    }

    @Override
    public boolean acceptAnswer(Long questionId, Long answerId) {
        // 设置问题状态为已解决
        return questionMapper.updateStatus(questionId, 1) > 0;
        // 注意：实际业务中还需要处理金币转账等逻辑
    }
}